package com.zivwu.utils.device;

import android.Manifest;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import androidx.annotation.RequiresPermission;

import com.zivwu.utils.AppUtils;

import java.net.SocketException;

public class DeviceHelper {


    /**
     * 获取设备的IMEI号 需要权限{@see android.Manifest.permission.READ_PHONE_STATE}
     */
    @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
    public static String getIMEI() {
        Application app = AppUtils.getApp();
        TelephonyManager mTelephonyMgr = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
        String imei = null;


        if (mTelephonyMgr != null) {
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                imei = mTelephonyMgr.getImei(); //获取IMEI号
            } else {
                imei = mTelephonyMgr.getDeviceId();
            }
        }
        return imei;
    }

    /**
     * 获取设备的IMSI号 需要权限{@see android.Manifest.permission.READ_PHONE_STATE}
     */
    @RequiresPermission(android.Manifest.permission.READ_PHONE_STATE)
    public static String getIMSI() {
        Application app = AppUtils.getApp();
        TelephonyManager mTelephonyMgr = (TelephonyManager) app.getSystemService(Context.TELEPHONY_SERVICE);
        String imsi = null;
        if (mTelephonyMgr != null)
            imsi = mTelephonyMgr.getSubscriberId(); //获取IMSI号
        return imsi;
    }


    /**
     * 获取Mac地址
     *
     * @return
     */
    @RequiresPermission(allOf = {Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.CHANGE_WIFI_STATE})
    public static String getMacAddress() {
        return MacHelper.getMacAddress();
    }


    /**
     * 获取Android ID
     *
     * @return String
     */
    public static String getAndroidId() {
        return Settings.System.getString(AppUtils.getApp().getContentResolver(), Settings.Secure.ANDROID_ID);
    }


    public static String getIpAddress() throws SocketException {
        return IpHelper.getLocalIpAddress();
    }

}
